package com.yhsimon.thread;

import java.util.concurrent.*;

/** 方式三：实现Callable接口
 * 1.可以定义返回值
 * 2.可以抛出异常
 */
public class TestCallable implements Callable<Boolean> {
    @Override
    public Boolean call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"已完成");
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable t1=new TestCallable();
        TestCallable t2=new TestCallable();
        TestCallable t3=new TestCallable();

        //创建执行服务
        ExecutorService ser= Executors.newFixedThreadPool(3);

        //提交执行
        Future<Boolean> r1=ser.submit(t1);
        Future<Boolean> r2=ser.submit(t2);
        Future<Boolean> r3=ser.submit(t3);

        //获取结果
        boolean rs1=r1.get();
        boolean rs2=r2.get();
        boolean rs3=r3.get();

        System.out.println(rs1+" "+rs2+" "+rs3);

        //关闭服务
        ser.shutdownNow();
    }
}
